﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using GameEngine.Camera;
using GameEngine.EntityComponents;

namespace GameEngine.Entities
{
    public abstract class AbstractDrawableEntity : AbstractEntity
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="AbstractDrawableEntity" /> class.
        /// </summary>
        /// <param name="components">The components.</param>
        /// <param name="isActive">if set to <c>true</c> [is active].</param>
        /// <param name="isVisible">if set to <c>true</c> [is visible].</param>
        /// <param name="position">The position.</param>
        /// <param name="game">The game.</param>
        public AbstractDrawableEntity(List<IEntityComponent> components, bool isActive, bool isVisible, Vector2 position, Game game)
            : base(components, isActive, isVisible, position, game)
        { }

        /// <summary>
        /// Updates the AbstractDrawableEntity
        /// </summary>
        /// <param name="gameTime">The game time.</param>
        public override void Update(GameTime gameTime)
        {
            if(this.isActive)
                UpdateComponents(gameTime);
        }

        /// <summary>
        /// Draws the AbstractDrawableEntity
        /// </summary>
        /// <param name="spriteBatch">The sprite batch.</param>
        /// <param name="gameTime">The game time.</param>
        public override void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            if (!this.isVisible)
                return;
            spriteBatch.Begin(SpriteSortMode.BackToFront,
                        BlendState.AlphaBlend,
                        null,
                        null,
                        null,
                        null,
                        ((Camera2D)game.Services.GetService(typeof(Camera2D))).GetTransformation());

            if (sourceRectangle == Rectangle.Empty)
                spriteBatch.Draw(texture, position, null, Color.White, rotation, origin, scale, effects, layerDepth);
            else
                spriteBatch.Draw(texture, position, sourceRectangle, Color.White, rotation, origin, scale, effects, layerDepth);

            spriteBatch.End();
        }
    }
}